/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.nnaplanner;

import com.appspot.nnaplanner.DAL.DALConnection;
import com.appspot.nnaplanner.DAL.DALGraph;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import org.datanucleus.jdo.state.LifeCycleStateFactory;
import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleDirectedWeightedGraph;

/**
 *
 * @author david
 */
public class NNappData {

    private static NNappData instance = null;
    private SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> _graph = null;

    private NNappData() {
        DALGraph dg = new DALGraph();
        _graph = dg.loadGraph();
    }

    public static SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> getGraph() {
        return getInstance()._graph;
    }

    public static NNappData getInstance() {
        if (instance == null) {
            instance = new NNappData();

        }
        return instance;
    }

    public static List<List<DALConnection>> getRoute(List<String> startids, List<String> endids) {
        //System.out.println("Starts:"  + startids + " \nends:" + endids);
        List<List<DALConnection>> routes = new ArrayList<List<DALConnection>>();
        try {
            for (String start : startids) {
                for (String end : endids) {
                    List<DALConnection> route = new ArrayList<DALConnection>();
                    List<DefaultWeightedEdge> path = DijkstraShortestPath.findPathBetween(getGraph(), start, end);
                    if (path != null) {
                        for (DefaultWeightedEdge edge : path) {
                            String[] ids = edge.toString().split(":");
                            ids[0] = ids[0].replace("(", "");
                            ids[0] = ids[0].replace(")", "");
                            ids[1] = ids[1].replace(")", "");
                            ids[1] = ids[1].replace("(", "");
                            DALConnection dc = new DALConnection();
                            dc.setStop(ids[0].trim());
                            dc.setDestination(ids[1].trim());
                            dc.setID(dc.getStopID() + dc.getDestinationID());
                            route.add(dc);
                        }
                        routes.add(route);
                    }
                }

            }
        } catch (Exception ex) {
            Logger.getAnonymousLogger().warning( ex.toString() +"\nStart ids:" + startids + "\n end ids " + endids);
        }
        return routes;
    }

    public static List<DALConnection> getRoute(String startid, String endid) {
        List<String> startids = new ArrayList<String>();
        List<String> endids = new ArrayList<String>();
        startids.add(startid);
        endids.add(endid);
        List<List<DALConnection>> result = getRoute(startids, endids);
        if (result.size() > 0) {
            return result.get(0);
        } else {
            return new ArrayList<DALConnection>();
        }
    }

    @Override
    protected void finalize() throws Throwable {
        java.util.logging.Logger.getLogger(this.toString()).info("Killing NNAPDATA: saving graph!");
        DALGraph dg = new DALGraph();
        dg.save(_graph);
        java.util.logging.Logger.getLogger(this.toString()).info("Killed NNAPDATA!");
        super.finalize();
    }
}
